import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.Keys;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


public class T_Spider {
    public String Spider(String S) {
        // 设置ChromeDriver的路径
        System.setProperty("webdriver.chrome.driver", "chromedriver.exe");

        // 启动Chrome浏览器
        WebDriver driver = new ChromeDriver();

        // 设置隐式等待时间
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        try {
            // 打开Boss直聘网站
            driver.get("https://www.zhipin.com/web/geek/job?city=101010100");
            TimeUnit.SECONDS.sleep(2);
            // 输入职位类型并搜索
            WebElement searchInput = driver.findElement(By.cssSelector("#wrap > div.page-job-wrapper > div.job-search-wrapper > div.job-search-box.clearfix > div > div > div > input"));
            searchInput.sendKeys(S); // 这里可以更换为你想搜索的职位类型
            searchInput.sendKeys(Keys.RETURN); // 模拟按下“Enter”键

            // 等待搜索结果加载
            TimeUnit.SECONDS.sleep(1);
            // 检查是否出现登录验证框
            WebDriverWait wait = new WebDriverWait(driver, 10);
            try {
                WebElement closeButton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.boss-login-dialog")));
                closeButton.click();
            } catch (Exception e) {
                // 如果找不到登录验证框，继续执行
                System.out.println("登录验证框未出现，继续执行后续操作。");
            }

            // 获取搜索结果中的前五个公司链接

            List<String> jobUrls = new ArrayList<>();
            for (int i = 0; i < 5; i++) {
                WebElement jobLinks = driver.findElement(By.cssSelector("#wrap > div.page-job-wrapper > div.page-job-inner > div > div.job-list-wrapper > div.search-job-result > ul > li:nth-child("+String.valueOf(i+1)+") > div.job-card-body.clearfix"));
                WebElement link = jobLinks.findElement(By.tagName("a"));
                jobUrls.add(link.getAttribute("href"));
            }

            // 存储任职要求的文本
            List<String> jobDescriptions = new ArrayList<>();

            // 依次打开每个公司的职位页面并抓取任职要求
            for (String url : jobUrls) {
                driver.get(url);
                // 等待搜索结果加载
                TimeUnit.SECONDS.sleep(2);
                // 检查是否出现登录验证框
                WebDriverWait wait1 = new WebDriverWait(driver, 10);
                try {
                    WebElement closeButton = wait1.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.boss-login-dialog")));
                    closeButton.click();
                } catch (Exception e) {
                    // 如果找不到登录验证框，继续执行
                    System.out.println("登录验证框未出现，继续执行后续操作。");
                }
                // 等待搜索结果加载
                TimeUnit.SECONDS.sleep(1);
                // 获取任职要求文本
                WebElement jobDescriptionElement = driver.findElement(By.cssSelector("#main > div.job-box > div > div.job-detail > div:nth-child(1)"));

                // 获取ul中的li元素文本
                List<WebElement> ulElements = jobDescriptionElement.findElements(By.tagName("ul"));
                StringBuilder ulText = new StringBuilder();
                for (WebElement ul : ulElements) {
                    List<WebElement> liElements = ul.findElements(By.tagName("li"));
                    for (WebElement li : liElements) {
                        ulText.append(li.getText()).append("\n");
                    }
                }

                // 存储文本内容
                StringBuilder fullText = new StringBuilder();
                WebElement smallDiv = jobDescriptionElement.findElement(By.cssSelector("#main > div.job-box > div > div.job-detail > div:nth-child(1) > div.job-sec-text"));
                fullText.append(getTextWithBrTags(smallDiv));

                // 合并ul和div的文本
                String fullDescription = ulText.toString() + "\n" + fullText.toString();
                jobDescriptions.add(fullDescription);

                // 等待一段时间以避免被反爬虫机制检测
                TimeUnit.SECONDS.sleep(2);
            }

            // 输出结果
            for (String description : jobDescriptions) {
                System.out.println("Job Description:");
                System.out.println(description);
                System.out.println("--------------------------------------------------");
            }
            String merge = String.join("\n",jobDescriptions);
            return merge;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭浏览器
            driver.quit();
        }
        return null;
    }
    // 方法：获取带有br标签的文本
    private static String getTextWithBrTags(WebElement element) {
        String innerHtml = element.getAttribute("innerHTML");
        return innerHtml.replaceAll("(?i)<br[^>]*>", "\n").replaceAll("<[^>]*>", "").trim();
    }
}
